package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dto.WantSellDTO;
import com.sc.nft.entity.ScrollWantBuy;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.enums.WantSellOrderStatusEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.ScrollWantSell;
import com.sc.nft.mapper.ScrollWantSellMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * 卷轴商店寄售表(sc_scroll_want_sell)数据DAO
 *
 * @author zxy
 * @since 2024-09-04 02:15:00
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class ScrollWantSellDao extends ServiceImpl<ScrollWantSellMapper, ScrollWantSell> {

    public Integer getCountByShopId(Long shopId) {
        return lambdaQuery().eq(ScrollWantSell::getShopId, shopId).eq(ScrollWantSell::getStatus, WantSellOrderStatusEnum.IN_BUY).count();
    }

    public Page<ScrollWantSell> myWantSellPage(Long  userId, Page mp) {
        return lambdaQuery().eq(ScrollWantSell::getShopUserId,userId).last(" order by field(status,1,2,0),id desc").page(mp);

    }

    public Page<WantSellDTO> wantSellPage(Long shopId, Page page) {
        return baseMapper.wantSellPage(page,shopId);
    }

    public void subFreeze(Long id, int count) {
        UpdateWrapper<ScrollWantSell> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("freeze_count=freeze_count-"+count);
        updateWrapper.eq("id", id);
        update(updateWrapper);
    }

    public void paySuccess(Long id, int count, BigDecimal price ,BigDecimal charge) {
        UpdateWrapper<ScrollWantSell> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("freeze_count=freeze_count-"+count+" , sales_count=sales_count+"+count+" , sales_price=sales_price+"+price+" ,sales_fee_amount=sales_fee_amount+"+charge);
        updateWrapper.eq("id", id);
        update(updateWrapper);
    }

    public void updateStatus(Long id, WantSellOrderStatusEnum wantSellOrderStatusEnum) {
        LambdaUpdateWrapper<ScrollWantSell> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ScrollWantSell::getStatus, wantSellOrderStatusEnum);
        updateWrapper.eq(ScrollWantSell::getId, id);
        update(updateWrapper);
    }

    public void addFreeze(Long id, Integer count) {
        UpdateWrapper<ScrollWantSell> wrapper = new UpdateWrapper<>();
        wrapper.setSql("freeze_count=freeze_count+"+count);
        wrapper.eq("id", id);
        update(wrapper);
    }
}